import * as api from '@/api/sourcing/demandInquery'

const state = {
  demandList: [],
  orderList: [],
  selePageList: [],
  swithpdpageList: [],
  remindList: {},
  remindTotalCount: 0,
  sourcebillList: [],
  total: 0,
  totalabre: 0,
  totalre: 0,
  ordertotal: 0,
  orderTableLoading: false,
  inqueryTableLoading: false,
  inqueryBtnLoading: false,
  acceptBtnLoading: false,
}

const mutations = {
  SET_DEMAND_LIST: (state, result) => {
    const { list = [], total = 0 } = result
    state.demandList = list
    state.total = total
  },
  SET_ORFER_LIST: (state, result) => {
    const { list = [], total = 0 } = result
    state.orderList = list
    state.ordertotal = total
  },
  SET_SOURCE_BILL_LIST: (state, sourcebillList) => {
    state.sourcebillList = sourcebillList
  },
  SET_EVENTREMIND_LIST: (state, remindList) => {
    state.remindList = remindList
    state.remindTotalCount = remindList.totalCount
  },
}
const actions = {
  // 首页待办事项
  eventRemindList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.eventRemindService(data).then((res) => {
      if (cb) {
        cb(res);
      }
      commit('SET_EVENTREMIND_LIST', res.data)
    });
  },
  // 询价需求---询价列表
  getDemandInqeryList ({
    state,
    commit
  }, {
    payload: {
      data,
      cb
    }
  }) {
    state.inqueryTableLoading = true;
    api.getDemandInqeryListService(data).then((res) => {
      state.inqueryTableLoading = false;
      if (cb) {
        cb(res);
      } else {
        state.demandList = []
      }
      commit('SET_DEMAND_LIST', res.data ? res.data : [])
    }).catch(() => {
      commit('SET_DEMAND_LIST', [])
      state.inqueryTableLoading = false;
    });
  },
  // 询价需求 询价--采购询价 生成询价单据
  sourceGenBillList ({
    state,
    commit
  }, {
    payload: {
      data,
      cb
    }
  }) {
    state.inqueryBtnLoading = true;
    return new Promise((resolve, reject) => {
      api.sourceGenBillService(data).then(res => {
        state.inqueryBtnLoading = false;
        if (cb) {
          cb(res)
        }
        commit('SET_SOURCE_BILL_LIST', res.data ? res.data : [])
        resolve(res.data)
      }).catch(error => {
        state.inqueryBtnLoading = false;
        reject(error)
      })
    })
  },
  // 询价需求 订货--采购询价 生成询价单据
  handleOrderInquiryList ({
    state,
    commit
  }, {
    payload: {
      data,
      cb
    }
  }) {
    state.inqueryBtnLoading = true;
    return new Promise((resolve, reject) => {
      api.handleOrderInquiryService(data).then(res => {
        state.inqueryBtnLoading = false;
        if (cb) {
          cb(res)
        }
        commit('SET_SOURCE_BILL_LIST', res.data ? res.data : [])
        resolve(res.data)
      }).catch(error => {
        state.inqueryBtnLoading = false;
        reject(error)
      })
    })
  },
  // 询价需求---转派人员保存
  requirementSwithList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.requirementSwithService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 订货需求---转派人员保存
  transferOrderGoodsList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.transferOrderGoodsService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 询价需求---询价异常接口 保存
  expInquiryList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.expInquiryService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 订货需求---询价异常接口 保存
  exceptionOrderGoodsList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.exceptionOrderGoodsService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 采购询价 --- 客户订货---取消---询价异常保存
  cancelOrderList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.cancelOrderService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 采购询价 --- 客户询价---取消---询价异常保存
  cancelInquiryList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.cancelInquiryService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 询价需求--- 询受理
  srmAcceptList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    state.acceptBtnLoading = true;
    api.srmAcceptService(data).then((res) => {
      state.acceptBtnLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.acceptBtnLoading = false;
    });
  },
  // 订货需求--- 询受理
  acceptanceList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    state.acceptBtnLoading = true;
    api.acceptanceService(data).then((res) => {
      state.acceptBtnLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.acceptBtnLoading = false;
    });
  },
  // 询价需求--- 取消受理
  srmCancelAcceptList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.srmCancelAcceptService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 订货需求--- 取消受理
  cancelAcceptanceList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.cancelAcceptanceService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 询价需求 ---询价 订货 --- 人员转派 ---部门
  queryHrDeptList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.queryHrDeptService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 询价需求 ---询价 订货 --- 人员转派 ---人员
  queryHrUserList ({
    commit,
    state
  }, {
    payload: {
      data,
      cb
    }
  }) {
    api.queryHrUserService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
